<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>palette.cpp Example File (mandelbrot/palette.cpp)</title>
  <link rel="stylesheet" type="text/css" href="style/style.css" />
  <script src="scripts/jquery.js" type="text/javascript"></script>
  <script src="scripts/functions.js" type="text/javascript"></script>
  <script src="./scripts/superfish.js" type="text/javascript"></script>
  <link rel="stylesheet" type="text/css" href="style/superfish.css" />  <script src="./scripts/narrow.js" type="text/javascript"></script>
  <link rel="stylesheet" type="text/css" href="style/narrow.css" />
  <!--[if IE]>
<meta name="MSSmartTagsPreventParsing" content="true">
<meta http-equiv="imagetoolbar" content="no">
<![endif]-->
<!--[if lt IE 7]>
<link rel="stylesheet" type="text/css" href="style/style_ie6.css">
<![endif]-->
<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="style/style_ie7.css">
<![endif]-->
<!--[if IE 8]>
<link rel="stylesheet" type="text/css" href="style/style_ie8.css">
<![endif]-->
</head>
<body class="" onload="CheckEmptyAndLoadList();">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="150"><img src="images/qt-logo.png" align="left" border="0" />&nbsp;&nbsp;<img src="images/qtopencl-logo.png" border="0" /></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"> <a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="qtopencl-all-classes.html"><font color="#004faf">All Classes</font></a></td>
</table><h1 class="title">palette.cpp Example File</h1>
<span class="small-subtitle">mandelbrot/palette.cpp</span>
<!-- $$$mandelbrot/palette.cpp-description -->
<div class="descr"> <a name="details"></a>
<pre class="highlightedCode brush: cpp"><span class="comment">    /****************************************************************************
    **
    ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
    ** All rights reserved.
    ** Contact: Nokia Corporation (qt-info@nokia.com)
    **
    ** This file is part of the QtOpenCL module of the Qt Toolkit.
    **
    ** $QT_BEGIN_LICENSE:LGPL$
    ** No Commercial Usage
    ** This file contains pre-release code and may not be distributed.
    ** You may use this file in accordance with the terms and conditions
    ** contained in the Technology Preview License Agreement accompanying
    ** this package.
    **
    ** GNU Lesser General Public License Usage
    ** Alternatively, this file may be used under the terms of the GNU Lesser
    ** General Public License version 2.1 as published by the Free Software
    ** Foundation and appearing in the file LICENSE.LGPL included in the
    ** packaging of this file.  Please review the following information to
    ** ensure the GNU Lesser General Public License version 2.1 requirements
    ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
    **
    ** In addition, as a special exception, Nokia gives you certain additional
    ** rights.  These rights are described in the Nokia Qt LGPL Exception
    ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
    **
    ** If you have questions regarding the use of this file, please contact
    ** Nokia at qt-info@nokia.com.
    **
    **
    **
    **
    **
    **
    **
    **
    ** $QT_END_LICENSE$
    **
    ****************************************************************************/</span>

    #include &quot;palette.h&quot;

    Palette::Palette()
        : type(QColor::Invalid), offset(0.0f)
    {
        setStandardPalette(Fire);
    }

    Palette::~Palette()
    {
    }

    void Palette::setStandardPalette(StandardPalette palette)
    {
        clear();
        switch (palette) {
        default: case Red:
            addHsv(0.0f, 0.0f, 1.0f, 1.0f);
            addHsv(1.0f, 1.0f, 1.0f, 1.0f);
            break;
        case Blue:
            addHsv(0.0f, 0.65f, 1.0f, 0.5f);
            addHsv(1.0f, 0.05f, 1.0f, 1.0f);
            break;

        <span class="comment">// The following palettes are from xMandelbrotViewer.</span>
        <span class="comment">// http://math.hws.edu/xJava/MB/</span>
        case EarthSky:
            addRgb(0.0f, 1.0f, 1.0f, 1.0f);
            addRgb(0.15f, 1.0f, 0.8f, 0.0f);
            addRgb(0.33f, 0.53f, 0.12f, 0.075f);
            addRgb(0.67f, 0.0f, 0.0f, 0.6f);
            addRgb(0.85f, 0.0f, 0.4f, 1.0f);
            addRgb(1.0f, 1.0f, 1.0f, 1.0f);
            setOffset(0.85f);
            break;
        case HotCold:
            addRgb(0.0f, 1.0f, 1.0f, 1.0f);
            addRgb(0.16f, 0.0f, 0.4f, 1.0f);
            addRgb(0.5f, 0.2f, 0.2f, 0.2f);
            addRgb(0.84f, 1.0f, 0.0f, 0.8f);
            addRgb(1.0f, 1.0f, 1.0f, 1.0f);
            break;
        case Fire:
            addRgb(0.0f, 0.0f, 0.0f, 0.0f);
            addRgb(0.17f, 1.0f, 0.0f, 0.0f);
            addRgb(0.83f, 1.0f, 1.0f, 0.0f);
            addRgb(1.0f, 1.0f, 1.0f, 1.0f);
            break;
        }
    }

    void Palette::clear()
    {
        type = QColor::Invalid;
        offset = 0.0f;
        ranges.clear();
    }

    void Palette::addRgb(qreal start, qreal r, qreal g, qreal b)
    {
        type = QColor::Rgb;
        Range range;
        range.start = start;
        range.r = r;
        range.g = g;
        range.b = b;
        ranges.append(range);
    }

    void Palette::addHsv(qreal start, qreal h, qreal s, qreal v)
    {
        type = QColor::Hsv;
        Range range;
        range.start = start;
        range.h = h;
        range.s = s;
        range.v = v;
        ranges.append(range);
    }

    QVector&lt;QRgb&gt; Palette::createTable(int size) const
    {
        Q_ASSERT(ranges.size() &gt;= 2);
        Q_ASSERT(ranges[ranges.size() - 1].start == 1.0f);
        QVector&lt;QRgb&gt; colors;
        colors.resize(size);
        int ioffset = int(offset * size);
        for (int index = 0; index &lt; size; ++index) {
            qreal amt = qreal((index + ioffset) % size) / (size - 1);
            int posn = 1;
            while (ranges[posn].start &lt; 1.0f &amp;&amp; ranges[posn].start &lt; amt)
                ++posn;
            amt = (amt - ranges[posn - 1].start) /
                  (ranges[posn].start - ranges[posn - 1].start);
            if (type == QColor::Rgb) {
                qreal r = ranges[posn - 1].r +
                    (ranges[posn].r - ranges[posn - 1].r) * amt;
                qreal g = ranges[posn - 1].g +
                    (ranges[posn].g - ranges[posn - 1].g) * amt;
                qreal b = ranges[posn - 1].b +
                    (ranges[posn].b - ranges[posn - 1].b) * amt;
                colors[index] = QColor::fromRgbF(r, g, b).rgb();
            } else {
                qreal h = ranges[posn - 1].h +
                    (ranges[posn].h - ranges[posn - 1].h) * amt;
                qreal s = ranges[posn - 1].s +
                    (ranges[posn].s - ranges[posn - 1].s) * amt;
                qreal v = ranges[posn - 1].v +
                    (ranges[posn].v - ranges[posn - 1].v) * amt;
                colors[index] = QColor::fromHsvF(h, s, v).rgb();
            }
        }
        return colors;
    }</pre>
</div>
<!-- @@@mandelbrot/palette.cpp -->
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td align="left">Copyright &copy; 2010 Nokia Corporation</td>
<td align="right">QtOpenCL Documentation</td>
</tr></table></div></address>  <script src="scripts/functions.js" type="text/javascript"></script>
  <script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-4457116-5']);
  _gaq.push(['_trackPageview']);
  (function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
  </script>
</body>
</html>
